#!usr/bin/env python
# -*- coding: utf-8 -*-
import time
from datetime import datetime


def time_to_string_for_before(date_time, use_ch_date_format=False):
    now_time = datetime.now()
    timedelta = now_time - date_time
    t_seconds = timedelta.days * 24 * 3600 + timedelta.seconds

    if t_seconds < 60:
        if t_seconds < 0:
            t_result = date_time.strftime('%Y-%m-%d %H:%M:%S')
        elif t_seconds <= 10:
            t_result = u"刚刚"
        else:
            t_result = str(t_seconds / 10 * 10) + u"秒前"
    else:
        if t_seconds / 60 <= 60:
            t_result = str(t_seconds / 60) + u"分钟前"
        elif t_seconds / 60 / 60 < 24:
            t_result = str(t_seconds / 60 / 60) + u"小时前"
        else:
            if use_ch_date_format:
                t_result = u'{0}年{1}月{2}日'.format(
                    date_time.year, date_time.month, date_time.day
                )
            else:
                t_result = date_time.strftime('%Y-%m-%d')
    return t_result


def str_time_string_for_before(str_time):
    date_time = str_time_to_datetime(str_time)
    return time_to_string_for_before(date_time)


def timestamp_to_string_for_before(timestamp):
    date_time = get_date_time_from_timestamp(timestamp)
    return time_to_string_for_before(date_time)


def str_time_to_datetime(str_time, format_str='%Y-%m-%d %H:%M:%S'):
    return datetime.strptime(str_time, format_str)


def datetime_to_str_time(date, format_str='%Y-%m-%d %H:%M:%S'):
    return date.strftime(format_str)


def get_now_str(format_str='%Y-%m-%d %H:%M:%S'):
    return datetime.now().strftime(format_str)


def get_now():
    return datetime.now()


def get_timestamp(date_time=None):
    if not date_time:
        date_time = datetime.now()
    return int(time.mktime(date_time.timetuple()))


def get_date_time_from_timestamp(timestamp):
    return datetime.fromtimestamp(timestamp)


def to_tomorrow_second():
    return 86400 - (int(time.time()) - time.timezone) % 86400
